# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class QkRb101Pipeline:

    def open_spider(self, spider):

        self.fp = open('read.json', 'w', encoding='utf-8')

    def process_item(self, item, spider):

        # 只能写入字符串  需要强制转换
        self.fp.write(str(item))
        return item

    def close_spider(self, spider):

        self.fp.close()


# 加载settings文件
from scrapy.utils.project import get_project_settings
import pymysql


# class MysqlPipeline:
#
#     def open_spider(self, spider):
#         settings = get_project_settings()
#         self.host = settings['DB_HOST']
#         self.port = settings['DB_PORT']
#         self.user = settings['DB_USER']
#         self.password = settings['DB_PASSWORD']
#         self.name = settings['DB_NAME']
#         self.charset = settings['DB_CHARSET']
#
#         self.connect()
#
#     def connect(self):
#         self.con = pymysql.connect(
#                 host=self.host,
#                 port=self.port,
#                 user=self.user,
#                 password=self.password,
#                 db=self.name,
#                 charset=self.charset
#             )
#
#         self.corsor = self.con.cursor()
#
#     def process_item(self, item, spider):
#
#         sql = 'insert into book(name, src) values("{}","{}")'.format(item['name'], item['src'])
#         # 执行sql语句
#         self.corsor.execute(sql)
#         # 提交
#         self.con.commit()
#
#         return item
#
#     def close_spider(self, spider):
#         self.corsor.close()
#         self.con.close()
